# -*- coding: utf-8 -*-

#Joao Alturas_6197
#2014_10_16

import csv
import sys
import numpy as np
import xlrd
import matplotlib.pyplot as plt



from sqlalchemy import create_engine

from sqlalchemy.orm import sessionmaker

from sqlalchemy import Column, Integer, String, Float, func

from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('sqlite:///treta1.db')

Base = declarative_base()

"""
#Create base dados 

"""
class Dados(Base):
	
	__tablename__ = 'dados'	
	
	id = Column(Integer, primary_key=True)						
	ano = Column(Integer)
	ipcGlobal = Column(Float) 	 
	varAnualTaxInfl = Column(Float)     
	remMinMensal = Column(Float)   
	remMaxMensal = Column(Float) 
	pibPercapitAnual = Column(Float) 
	rendNacBrutPercAnual = Column (Float)  
	rendDispBrutPercAnual = Column (Float)  
	remunPercapitAnual = Column (Float) 
	
	
    
	def __init__(self,ano,ipcGlobal,varAnualTaxInfl,remMinMensal,remMaxMensal,
		pibPercapitAnual,rendNacBrutPercAnual,rendDispBrutPercAnual,
                     remunPercapitAnual):
											 
			self.ano = ano
			try:
				self.ipcGlobal = float(ipcGlobal)	
			except:
				self.ipcGlobal = 0
			try: 
				self.varAnualTaxInfl = float(varAnualTaxInfl)
			except:
				self.varAnualTaxInfl = 0
			try:
				self.remMinMensal = float(remMinMensal)
			except:
				self.remMinMensal = 0
			try:
				self.remMaxMensal = float(remMaxMensal)
			except:
				self.remMaxMensal= 0
			try:
				self.pibPercapitAnual = float(pibPercapitAnual)
			except:
				self.pibPercapitAnual = 0
			try:
				self.rendNacBrutPercAnual = float(rendNacBrutPercAnual)
			except:
				self.rendNacBrutPercAnual = 0
			try:
				self.rendDispBrutPercAnual = float(rendDispBrutPercAnual)
			except:
				self.rendDispBrutPercAnual = 0
			try:
				self.remunPercapitAnual = float(remunPercapitAnual)
			except:
				self.remunPercapitAnual = 0
            
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()


"""
Ler dados do ficheiro

"""
xls = xlrd.open_workbook('csvFile.xls')
plan = xls.sheets()[0]

for i in xrange(plan.nrows):
        row = plan.row_values(i)
	try:
                dado = Dados(row[1], row[2],row[3], row[4], row[5], row[6], row[7], row[8], row[9])
		session.add(dado)
	except Exception, err:
		print err
session.commit()



